/*
 * 仅供学习交流，严禁用于商业用途
 * */
package com.ssh.demo.base.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 后台Action基类
 * 
 * @author kai
 *
 */
public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private HttpServletRequest request;

	private HttpServletResponse response;
	
	private HttpSession session;
	
	protected Logger log = Logger.getLogger(this.getClass());
	
	protected String id;
	
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public String ajax(Object obj){
		PrintWriter out=null;
		try {
			response=initResponse("text/json;charset=UTF-8");
			out=response.getWriter();
			out.print(JSON.toJSONString(obj));
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (out!=null) {
				out.close();
			}
		}
		return NONE;
	}
	
	public HttpServletResponse initResponse(String contentType){
		try {
			response=ServletActionContext.getResponse();
			response.setHeader( "Pragma", "no-cache" );
			response.addHeader( "Cache-Control", "must-revalidate" );
			response.addHeader( "Cache-Control", "no-cache" );
			response.addHeader( "Cache-Control", "no-store" );
			response.setDateHeader("Expires", 0);
			response.setContentType(contentType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//response.set
		return response;
	}
}
